package edu.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import edu.core.ApplicationException;
import edu.core.PaginationSortOrderData;
import edu.core.StringUtil;
import edu.entity.User;

/**
 * Action基类.
 *
 * @author weidong
 *
 */
@SuppressWarnings("serial")
@Controller("baseAction")
public class BaseAction extends ActionSupport {
	/**
	 * 用于表现层输出错误信息.
	 */
	private String errorMsg;
	protected static final String EXCEPTION = "exception";			// 全局错误
	
	/**
	 * 前台分页Bean
	 */
	protected PaginationSortOrderData frontPage = new PaginationSortOrderData();

	/**
	 * 获取请求参数值.
	 *
	 * @author weidong
	 */
	public Map getRequestParamToMap() {
		System.out.println("获取请求参数开始，参数列表如下：");
		Map dataMap = new HashMap();
		// 获取请求参数key map
		Map paramMap = this.getRequest().getParameterMap();
		Set set = paramMap.keySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			String key = (String)it.next();
			String value = this.getRequest().getParameter(key);
			System.out.println(key+"=" + value);
			dataMap.put(key, value);
		}
		return dataMap;
	}
	
	/**
	 * 根据key从MAP中取值.
	 *
	 * @param key
	 * @return
	 * @author weidong
	 */
	public String getKeyValueFromMap(String key, Map map) {
		return (String)map.get(key);
	}
	
	/**
	 * 检测map中的某个key值是否为空,为空自动扔异常.
	 *
	 * @param key
	 * @author weidong
	 */
	public void validateNullException(Map paramMap, String key, String errorMsg) throws ApplicationException {
		if (paramMap == null) {
			throw new ApplicationException("目标map为空!");
		}
		String value = (String)paramMap.get(key);
		if (StringUtil.isEmpty(value)) { // 值为空
			throw new ApplicationException(errorMsg);
		}
	}

	/**
	 * 获得request.
	 * 
	 * @return httpservletRequest
	 * @author weidong
	 */
	public final HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得response.
	 *
	 * @return httpservletResponse
	 * @author weidong
	 */
	public final HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获得servlet上下文.
	 * 
	 * @return servletContext
	 */
	public final ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	/**
	 * 获得RealPath.
	 * @param path
	 * @return
	 */
	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

    /**
	 * 取得request单个参数值.
	 *
	 * @return value
	 * @author weidong
	 */
    public String getParameter(String key) {
        return getRequest().getParameter(key);
    }
    
    /**
	 * 取得request同名参数值.
	 * 
	 * @return value
	 * @author weidong
	 */
    public String[] getParameterValues(String key) {
        return getRequest().getParameterValues(key);
    }
    
    /**
	 * 获取登录用户信息.
	 *
	 * @return 登录用户对象
	 * @author weidong
	 */
    public User getLoginUser() {
    	return (User) this.getHttpSession().getAttribute("user");
    }

    /**
	 * 获取HttpSession.
	 *
	 * @return httpsession
	 * @author weidong
	 */
    public HttpSession getHttpSession() {
    	return this.getRequest().getSession();
    }

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public PaginationSortOrderData getFrontPage() {
		return frontPage;
	}

	public void setFrontPage(PaginationSortOrderData frontPage) {
		this.frontPage = frontPage;
	}

	public static String getException() {
		return EXCEPTION;
	}
}
